package com.motionlog.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.motionlog.R;
import com.motionlog.model.Base;
import com.motionlog.model.Database;

public class NewItem extends Activity {

	protected static final String TAG = NewItem.class.getSimpleName();

	Handler handler = new Handler();
	Button motion;
	Button source;
	SeekBar level;
	TextView mSummary;
	String summary;

	Base curentMotion = null;
	ArrayList<Base> reasons = new ArrayList<Base>();

	Database mDb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newitem);

		mDb = Database.getInstance(this);

		level = (SeekBar) findViewById(R.id.level);

		mSummary = (TextView) findViewById(R.id.summary);

		motion = (Button) findViewById(R.id.motion);
		motion.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(NewItem.this,
						SelectMotion.class), 0);
			}
		});
		source = (Button) findViewById(R.id.source);
		source.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(NewItem.this, SelectReason.class);
				startActivityForResult(i, 1);
			}
		});

		Button putEnvl = (Button) findViewById(R.id.putEnvl);
		putEnvl.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				android.util.Log.d(TAG, "curretnMotion: " + curentMotion
						+ ", resion.size: " + reasons.size());
				if (curentMotion == null || reasons.size() == 0) {
					Toast.makeText(NewItem.this, "请选择相应情绪或来源",
							Toast.LENGTH_LONG).show();
					return;
				}

				final ProgressDialog loading = new ProgressDialog(NewItem.this);
				loading.setMessage("计算中...");
				loading.show();

				// final long rowId = mDb.insertLog(curentMotion.id,
				// level.getProgress(), summary);
				// for (Base reason : reasons) {
				// mDb.insertReasonLog(rowId, reason.id);
				// }

				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						loading.cancel();
						Intent i = new Intent(NewItem.this,
								NewItemAddition.class);
						// i.putExtra("logId", rowId);
						i.putExtra("motion_id", curentMotion.id);
						i.putExtra("level", level.getProgress());
						i.putExtra("summary", summary);
						i.putExtra("reasons", reasons);
						startActivity(i);
					}
				}, 3000);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 0:
			if (resultCode == RESULT_OK) {
				Base m = (Base) data.getParcelableExtra("motion");
				motion.setText(m.name);
				curentMotion = m;
			}
			break;
		case 1:
			if (resultCode == RESULT_OK) {
				summary = data.getStringExtra("summary");
				reasons = data.getParcelableArrayListExtra("reasons");
				mSummary.setText(summary);
			}
			break;
		default:
			break;
		}
	}
}
